package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.app.domain.bo.AppVipCardBo;
import com.ruoyi.app.domain.vo.AppVipCardVo;
import com.ruoyi.app.domain.AppVipCard;
import com.ruoyi.app.mapper.AppVipCardMapper;
import com.ruoyi.app.service.IAppVipCardService;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 广告卡列表Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-04
 */
@RequiredArgsConstructor
@Service
public class AppVipCardServiceImpl implements IAppVipCardService {

    private final AppVipCardMapper baseMapper;

    /**
     * 查询广告卡列表
     */
    @Override
    public AppVipCardVo queryById(Long cardId) {
        return baseMapper.selectVoById(cardId);
    }

    @Override
    public AppVipCardVo queryCardNumber(String cardNumber) {
        LambdaQueryWrapper<AppVipCard> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppVipCard::getCardNumber, cardNumber);
        return baseMapper.selectVoOne(wrapper);
    }

    /**
     * 查询广告卡列表列表
     */
    @Override
    public TableDataInfo<AppVipCardVo> queryPageList(AppVipCardBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppVipCard> lqw = buildQueryWrapper(bo);
        Page<AppVipCardVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<AppVipCardVo> queryList(LambdaQueryWrapper<AppVipCard> wrapper) {
        return baseMapper.selectVoList(wrapper);
    }

    /**
     * 查询广告卡列表列表
     */
    @Override
    public List<AppVipCardVo> queryList(AppVipCardBo bo) {
        LambdaQueryWrapper<AppVipCard> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppVipCard> buildQueryWrapper(AppVipCardBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppVipCard> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCardName()), AppVipCard::getCardName, bo.getCardName());
        lqw.eq(StringUtils.isNotBlank(bo.getCardNumber()), AppVipCard::getCardNumber, bo.getCardNumber());
        lqw.eq(bo.getCardEndTime() != null, AppVipCard::getCardEndTime, bo.getCardEndTime());
        lqw.eq(bo.getCardUser() != null, AppVipCard::getCardUser, bo.getCardUser());
        lqw.eq(StringUtils.isNotBlank(bo.getStaus()), AppVipCard::getStaus, bo.getStaus());
        lqw.eq(bo.getUserId() != null, AppVipCard::getUserId, bo.getUserId());
        return lqw;
    }

    /**
     * 新增广告卡列表
     */
    @Override
    public Boolean insertByBo(AppVipCardBo bo) {
        AppVipCard add = BeanUtil.toBean(bo, AppVipCard.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCardId(add.getCardId());
        }
        return flag;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean inserts(List<AppVipCard> list) {
        return baseMapper.insertBatch(list);
    }

    /**
     * 修改广告卡列表
     */
    @Override
    public Boolean updateByBo(AppVipCardBo bo) {
        AppVipCard update = BeanUtil.toBean(bo, AppVipCard.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppVipCard entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除广告卡列表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean sendCard(List<AppVipCardVo> resultCardVos, Long id) {
        ArrayList<AppVipCard> list = new ArrayList<>();
        for (AppVipCardVo vo : resultCardVos) {
            AppVipCard appVipCard = new AppVipCard();
            appVipCard.setCardId(vo.getCardId());
            appVipCard.setCardUser(id);
            list.add(appVipCard);
        }
        return baseMapper.updateBatchById(list);
    }
}
